package zsc.liangyi.blog.dao;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
import zsc.liangyi.blog.model.domain.Authority;
import zsc.liangyi.blog.model.domain.Statistic;
import zsc.liangyi.blog.model.domain.User;

import java.util.List;

@Mapper
@Component
public interface UserDao {
    //根据id查找用户
    @Select("SELECT * FROM user WHERE id=#{id}")
    public User selectUserWithId(Integer id);

    //查找全部
    @Select("SELECT * FROM user ")
    public List<User> getAllUser();


    //根据username查找用户
    @Select("select * from user where username=#{username}")
    public User getUserByName(String username);

    //查询用户权限
    @Select("select a.* from " +
            "user c, authority a,user_authority ca " +
            "where ca.user_id=c.id and ca.authority_id=a.id and c.username=#{username}")
    public List<Authority> getAuthority(String username);


    //添加用户
    @Insert("INSERT INTO `user`(username,password,email,created,valid,signature) VALUES(#{username},#{password},#{email},#{created},#{valid},#{signature})")
    @Options(useGeneratedKeys = true,keyProperty="id",keyColumn = "id")//返回主键到实体类中
    public Integer addUser(User user);
    //为新建用户分配权限
    @Insert("INSERT INTO `user_authority`(user_id,authority_id) VALUES(#{userId},#{authorityId})")
    public Integer addAuthorityByUser(Integer userId,Integer authorityId);

    //更改用户信息
    public Integer update(User user);
}
